<template>
	<view class="categorize-container">
		<view class="search flex-sb">
			<u-search placeholder="搜索商品..." height="30" :clearabled="true" :showAction="false" v-model="keyword">
			</u-search>
		</view>
		<view class="tree-select">
			<view class="select-title">全部类目</view>
			<view class="select-content" >
				<view class="item" v-for="(item,index) in cateData" :key="index" @click="goToCateLink(item.id)">
					<view class="item-image">
						<image class="image" :src="item.image.url">
						</image>
					</view>
					<view class="item-text">{{item.name}}</view>
				</view>
			</view>
		</view>
		<unt-tab-bar :index="1"></unt-tab-bar>
	</view>
</template>

<script>
	import * as Cate from '@/api/cate/index.js'
	// 最后一次刷新时间
	let lastRefreshTime;
	export default {
		data() {
			return {
				isLoading: false,
				keyword: "",
				// 分类菜单
				cateData: [],
			}
		},
		onLoad() {
			// 加载页面数据
			this.onRefreshPage()
		},
		onShow() {
			// 每间隔5分钟自动刷新一次页面数据
			const curTime = new Date().getTime()
			if ((curTime - lastRefreshTime) > 5 * 60 * 1000) {
				this.onRefreshPage()
			}
		},
		methods: {
			// 刷新页面
			onRefreshPage() {
				// 记录刷新时间
				lastRefreshTime = new Date().getTime()
				// 获取页面数据
				this.getPageData()
			},
			// 获取页面数据
			getPageData() {
				const app = this
				app.isLoading = true
				Promise.all([Cate.categoryTree()])
					.then(result => {
						// 初始化分类列表数据
						app.cateData=result[0].data;
					})
					.finally(() => app.isLoading = false)
			},
			goToCateLink(ids){
				this.$unts.navTo("pages/categorize/cateshop/cateshop",{id:ids})
			},
		},
		
		   /**
		     * 下拉刷新
		     */
		    onPullDownRefresh() {
		      // 获取首页数据
		      this.getPageData(() => {
		        uni.stopPullDownRefresh()
		      })
		    },
		/**
		 * 设置分享内容
		 */
		onShareAppMessage() {
			const app = this
			return {
				title: _this.templet.shareTitle,
				path: '/pages/category/index?' + app.$unts.getShareUrlParams()
			}
		},

		/**
		 * 分享到朋友圈
		 * 本接口为 Beta 版本，暂只在 Android 平台支持，详见分享到朋友圈 (Beta)
		 * https://developers.weixin.qq.com/miniprogram/dev/framework/open-ability/share-timeline.html
		 */
		onShareTimeline() {
			const app = this
			return {
				title: _this.templet.shareTitle,
				path: '/pages/category/index?' + app.$unts.getShareUrlParams()
			}
		}
	}
</script>

<style lang="scss" scoped>
	.categorize-container {
		position: relative;
		width: 100%;
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
		margin-bottom: 160rpx;

		.search {
			width: 100%;
			padding: 10rpx 20rpx 10rpx 20rpx;
		}

		.tree-select {
			width: 100vw;
			padding: 0rpx 20rpx;
			margin-top: 10rpx;

			.select-title {
				font-size: 28rpx;
				font-weight: bold;
			}

			.select-content {
				margin-top: 30rpx;
				width: 100%;
				display: flex;
				align-items: flex-start;
				flex-shrink: 0;
				flex-wrap: wrap;

				.item {
					width: calc(100% / 4);
					margin-bottom: 40rpx;
					display: flex;
					justify-content: center;
					align-items: center;
					flex-direction: column;

					.item-image {
						width: 120rpx;
						height: 120rpx;
						border-radius: 50%;

						.image {
							width: 100%;
							height: 100%;
						}
					}

					.item-text {
						font-size: 28rpx;
						height: 32rpx;
						margin-top: 10rpx;
						overflow: hidden;
						text-align: center;
						text-overflow: ellipsis;
						white-space: nowrap;
						word-break: break-all;
					}
				}
			}
		}

	}
</style>